package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.SelectResultVo;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjBrand;
import com.zb.mapper.ZjBrandMapper;
import com.zb.result.PageUtil;
import com.zb.service.*;
import com.zb.utils.RandomUtil;
import com.zb.vo.BrandListVo;
import com.zb.vo.BrandParamVo;
import com.zb.vo.BrandSelectVo;
import com.zb.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-03
 */
@Service
public class ZjBrandServiceImpl extends ServiceImpl<ZjBrandMapper, ZjBrand> implements ZjBrandService {
    @Resource
    private ZjBrandMapper brandMapper;
    @Resource
    private ZjBrandParamService brandParamService;
    @Resource
    private ZjBrandDescribeService brandDescribeService;
    @Resource
    private ZjProductService productService;
    @Resource
    private ZjSpareService spareService;

    // 新增或修改商品品牌
    @Override
    @Transactional
    public void save(Long custAccId, BrandVo brandVo) {
        Assert.isTrue(null == brandMapper.existByName(custAccId, brandVo.getId(), brandVo.getName()), "品牌名称已重复！", brandVo.getName());
        if (StringUtils.isNotBlank(brandVo.getCode())) {
            Assert.isTrue(null == brandMapper.existByCode(custAccId, brandVo.getId(), brandVo.getCode()), "品牌编码已重复！", brandVo.getCode());
        }
        ZjBrand brand = new ZjBrand();
        BeanUtils.copyProperties(brandVo, brand);
        brand.setDisabled(brandVo.getStatus());
        if (null != brand.getId()) {
            this.updateById(brand);
            if (null != brandVo.getBrandParamIds() && !brandVo.getBrandParamIds().isEmpty()) {
                brandParamService.removeByIds(brandVo.getBrandParamIds());
            }
            brandParamService.save(custAccId, brand.getId(), brandVo.getBrandParamVos());
            brandDescribeService.updateById(brand.getId(), brandVo.getDescribe());
            productService.updateBrandByBrandId(brand.getId(), brand.getName());
            spareService.updateBrandByBrandId(brand.getId(), brand.getName());
            return;
        }
        brand.setId(IdWorker.getId());
        brand.setCustAccId(custAccId);
        brand.setCode(StringUtils.isNotBlank(brand.getCode())? brand.getCode() : "B" + RandomUtil.random28());
        brand.setTelephone(null == brand.getTelephone()? "" : brand.getTelephone());
        this.save(brand);
        brandParamService.save(custAccId, brand.getId(), brandVo.getBrandParamVos());
        brandDescribeService.save(custAccId, brand.getId(), brandVo.getDescribe());
    }

    // 获取品牌列表
    @Override
    public Map<String, Object> getList(Long custAccId, BrandSelectVo selectVo) {
        QueryWrapper<ZjBrand> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId);
        wrapper.orderByDesc("gmt_update");
        if (null != selectVo.getStatus()) {
            wrapper.eq("is_disabled", selectVo.getStatus());
        }
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        if (StringUtils.isNotBlank(selectVo.getCode())) {
            wrapper.likeRight("code", selectVo.getCode());
        }
        PageUtil.setPageLimit(selectVo);
        Page<ZjBrand> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);

        List<BrandListVo> voList = new ArrayList<>();
        page.getRecords().forEach(t -> voList.add(BrandListVo.beanToVo(t)));
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 获取品牌信息
    @Override
    public BrandVo getInfo(Long id) {
        ZjBrand brand = this.getById(id);
        Assert.isTrue(null != brand, "参数[" + id + "]的数据不存在！", null);

        BrandVo vo = new BrandVo();
        BeanUtils.copyProperties(brand, vo);
        vo.setStatus(brand.getDisabled());
        vo.setCreateTime(brand.getGmtCreate());
        vo.setUpdateTime(brand.getGmtUpdate());
        vo.setDescribe(brandDescribeService.getInfoById(id));

        List<BrandParamVo> paramVos = new ArrayList<>();
        brandParamService.getByBrandId(id).forEach(t -> {
            paramVos.add(new BrandParamVo(t.getId(), t.getName(), t.getVal()));
        });
        vo.setBrandParamVos(paramVos);
        return vo;
    }

    // 批量禁用或启用品牌
    @Override
    public void updateBatchDisableByIds(List<Long> idList, boolean status) {
        brandMapper.updateBatchDisableByIds(idList, status);
    }

    // 批量删除品牌
    @Override
    public void deleteByIds(List<Long> idList) {
        List<String> brands = productService.getRelationBranByBrandIds(idList);
        Assert.isTrue(brands.isEmpty(), "品牌[" + String.join("、", brands) + "]已关联产品，不可删除");
        brands = spareService.getRelationBranByBrandIds(idList);
        Assert.isTrue(brands.isEmpty(), "品牌[" + String.join("、", brands) + "]已关联配件，不可删除");
        brandParamService.deleteBatchByBrandIds(idList);
        brandDescribeService.removeByIds(idList);
        this.removeByIds(idList);
    }

    // 分页获取品牌数据下拉列表
    @Override
    public Map<String, Object> pageSelect(Long custAccId, SelectVo selectVo) {
        QueryWrapper<ZjBrand> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name");
        wrapper.eq("cust_acc_id", custAccId);
        wrapper.eq("is_disabled", false);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setSelectPageLimit(selectVo);
        Page<ZjBrand> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<SelectResultVo> voList = new ArrayList<>();
        page.getRecords().forEach(t -> voList.add(new SelectResultVo(t.getId(), t.getName())));
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }
}
